// 只针对一类业务的操作 - 学生
const prefix = '/api/student/' // 接口前缀
const studentDao = require('./api.student.dao') // 学生dao
const SqliteDB = require('./sqlite').SqliteDB // 数据库

function openDB () {
    return new SqliteDB("xian.db")
}
function closeDB (sqliteDB) {
    sqliteDB.close()
}
function response (db, res, data) {
    const code = 200;
    const msg = '';
    const time = +(new Date);
    const result = {code, data, msg, time};
    closeDB(db)
    return res.end(JSON.stringify(result));
}
module.exports = function (app) {
    app.get(prefix + 'createTable', (req, res) => {
        const db = openDB()
        studentDao.isTableExite(db).then(function () {
            response(db, res, { 'res_code': 0, 'res': '表格已经存在' })
        }, function () {
            studentDao.create(db).then(function () {
                response(db, res, { 'res_code': 1, 'res': '创建成功' })
            }, function () {
                response(db, res, { 'res_code': 0, 'res': '创建失败' })
            })
        })
    })
    app.get(prefix + 'dropTable', (req, res) => {
        const db = openDB()
        studentDao.isTableExite(db).then(function () {
            studentDao.drop(db).then(function () {
                response(db, res, { 'res_code': 1, 'res': '删除成功' })
            }, function () {
                response(db, res, { 'res_code': 0, 'res': '删除失败' })
            })
        }, function () {
            response(db, res, { 'res_code': 0, 'res': '表格不存在' })
        })
    })

    app.get(prefix + 'mockAddOne', (req, res) => {
        const db = openDB()
        studentDao.mockInsert(db).then(function () {
            response(db, res, { 'res_code': 1, 'res': '添加假数据成功' })
        }, function () {
            response(db, res, { 'res_code': 0, 'res': '添加假数据失败' })
        })
    })
    app.get(prefix + 'addOne', (req, res) => {
        const db = openDB()
        if (req.body.id) {
            const insertData = [
                req.body.id,
                req.body.name,
                req.body.sex,
                req.body.state,
                req.body.age,
                req.body.stay,
                req.body.desc,
                req.body.good,
                req.body.bad,
                '13582349999',
                '学生,在校'
            ]
            studentDao.insert(db, insertData).then(function () {
                response(db, res, { 'res_code': 1, 'res': '添加数据成功' })
            }, function () {
                response(db, res, { 'res_code': 0, 'res': '添加数据失败' })
            })
        } else {
            studentDao.update(db,
                req.body.id,
                req.body.name,
                req.body.sex,
                req.body.state,
                req.body.age,
                req.body.stay,
                req.body.desc,
                req.body.good,
                req.body.bad,
                '13582349999',
                '学生,在校').then(function () {
                response(db, res, { 'res_code': 1, 'res': '修改数据成功' })
            }, function () {
                response(db, res, { 'res_code': 0, 'res': '修改数据失败' })
            })
        }
    })

    app.get(prefix + 'list', (req, res) => {
        const db = openDB()
        studentDao.list(db).then(function (list) {
            studentDao.count(db).then(function (count) {
                response(db, res, { 'list': list, 'rowcount': count[0]['count(*)'] })
            }, function () {
                response(db, res, { 'list': [], 'rowcount': 0 })
            })
        }, function () {
            response(db, res, { 'list': [], 'rowcount': 0 })
        })
    })
    app.get(prefix + 'query', (req, res) => {
        const db = openDB()
        studentDao.query(db, req.query.id).then(function (info) {
            response(db, res, { 'info': info })
        }, function () {
            response(db, res, { 'info':{} })
        })
    })

    app.get(prefix + 'del', (req, res) => {
        const db = openDB()
        studentDao.del(db, req.query.id).then(function (count) {
            response(db, res, { 'res_code': 1, 'res': '删除数据成功' })
        }, function () {
            response(db, res, { 'res_code': 0, 'res': '删除数据失败' })
        })
    })
};
